package com.example.demo.ServiceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.example.demo.Mapper.Employee1Mapper;
import com.example.demo.POJO.tEmployee1;
import com.example.demo.Service.Employee1Service;

@Service
public class Employee1ServiceImpl implements Employee1Service{

	//给此类引入日志
	private static final Logger LOGGER = LoggerFactory.getLogger(Employee1ServiceImpl.class);
	
	@Autowired
	private Employee1Mapper emp;
	
	@Autowired
	private BCryptPasswordEncoder encoder;
	
	//不用@Autowired注解，因为存在泛型而用不了<String, tEmployee1>，而不用泛型也有问题
	@Resource
	private RedisTemplate<String, tEmployee1> redisTemplate;
	
	@Override
	public int addEmployee(tEmployee1 employee1) {
		// TODO Auto-generated method stub
		employee1.setPassword(encoder.encode(employee1.getPassword()));
		return emp.addEmployee(employee1);
	}

	@Override
	public List<tEmployee1> findAll() {
		// TODO Auto-generated method stub
		return emp.findAll();
	}

	@Override
	public int deleteEmployee(Integer eid) {
		// TODO Auto-generated method stub
		int i = emp.deleteEmployee(eid);
		//缓存存在，删除缓存
		String keyString = "employee1_"+eid;
		ValueOperations<String, tEmployee1> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(keyString);
		if (hasKey) {
			tEmployee1 employee1 = operations.get(keyString);
			//Employee1 employee1 = (Employee1) redisTemplate.opsForValue().get(keyString);
			redisTemplate.delete(keyString);
			LOGGER.info("Employee1ServiceImpl.findById() : 从缓存中删除了员工 >> " + employee1.toString());
		}
		return i;
	}

	@Override
	public int updateEmployee(tEmployee1 employee1) {
		// TODO Auto-generated method stub
		//先进行密码加密，后添加数据到DB中
		employee1.setPassword(encoder.encode(employee1.getPassword()));
		int i = emp.updateEmployee(employee1);
		//缓存存在，删除缓存
		String keyString = "employee1_"+employee1.getEid();
		ValueOperations<String, tEmployee1> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(keyString);
		if (hasKey) {
			employee1 = operations.get(keyString);
			//Employee1 employee1 = (Employee1) redisTemplate.opsForValue().get(keyString);
			redisTemplate.delete(keyString);
			LOGGER.info("Employee1ServiceImpl.findById() : 从缓存中删除了员工 >> " + employee1.toString());
		}
		return i;
	}

	@Override
	public tEmployee1 findById(Integer eid) {
		// TODO Auto-generated method stub
		//从缓存中获取员工信息
		String keyString = "employee1_"+eid;
		ValueOperations<String, tEmployee1> operations = redisTemplate.opsForValue();
		tEmployee1 employee1 = new tEmployee1();
		boolean hasKey = redisTemplate.hasKey(keyString);
		//如果缓存存在
		if (hasKey) {
			employee1 = operations.get(keyString);
			LOGGER.info("Employee1ServiceImpl.findById() : 从缓存中获取了员工 >> " + employee1.toString());
		} else {
            //从DB中获取城市信息
			System.out.println("无缓存，需要从数据库中查找");
			employee1 = emp.findById(eid);
			//插入缓存
			operations.set(keyString, employee1);
			LOGGER.info("Employee1ServiceImpl.findById() : 员工插入缓存 >> " + employee1.toString());

		}
		return employee1;
	}

	@Override
	public String equalPassword(Integer eid, String password) {
		// TODO Auto-generated method stub
		tEmployee1 employee1 = new tEmployee1();
		employee1 = emp.equalPassword(eid);
		if (encoder.matches(password, employee1.getPassword())) {
			return "登录成功！";
		} else {
			return "登录失败！";
		}
	}

}
